//
//  HSDrawerController.swift
//  HomeSchoolDemo
//
//  Created by 张晓林 on 16/9/27.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    
    // MARK: - 属性
    // 左边视图
    private let leftViewVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")
    // 添加标签控制器
    fileprivate let tabBarVc = HSTabBarController()
    // 动画时长
    fileprivate let animationTime: TimeInterval = 0.5
    // 标签视图的动画移动最大tx值
    fileprivate let barOffset = screenW * 0.842
    // 左边视图的动画移动最大tx值
    fileprivate let leftViewOffset = screenW / 2
    // 计算两个视图的移动比例
    fileprivate var offsetScale: CGFloat {
        return leftViewOffset / barOffset
    }
    
    // 记录第一次触摸点
    fileprivate var currentP: CGPoint?
    
    // 懒加载遮盖视图
    fileprivate lazy var coverView: UIButton = {
        // 创建按钮
        let btn = UIButton(frame: UIScreen.main.bounds)
        btn.alpha = 0.0
        btn.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        // 监听方法
        btn.addTarget(self, action: #selector(leftAnimation), for: .touchUpInside)
        // 设置透明
        return btn
    }()
    
    // MARK: - UI设置
    // 程序启动
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置UI效果
        setupUI()
        // 添加手势
        let panGesture = UIPanGestureRecognizer(target: self,
                                                action: #selector(panGestureFunc(gesture:)))
        view.addGestureRecognizer(panGesture)
    }
    
    // 设置UI
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.white
        // 添加抽屉效果子控制器
        // 添加控制器到管理控制器
        addChildViewController(leftViewVc)
        // 设置frame
        leftViewVc.view.frame = CGRect(x: 0, y: 0, width: barOffset - 3, height: screenH)
        // 设置边界阴影
        leftViewVc.view.layer.shadowColor = UIColor.black.cgColor
        leftViewVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftViewVc.view.layer.shadowOpacity = 0.9
        // 同时添加视图
        view.addSubview(leftViewVc.view)
        // 设置默认的形变偏移量
        leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
        
        // 添加标签视图控制器
        addChildViewController(tabBarVc)
        view.addSubview(tabBarVc.view)
        
        // 获得导航左边按钮的监听事件
        tabBarVc.leftBtnBlock = {() -> () in
            // transform.tx 就是视图改变的X值
            if self.tabBarVc.view.transform.tx == 0 {
                self.rightAnimation()
            }else {
                self.leftAnimation()
            }
        }
    }
    
    // MARK: - 抽屉效果动画方法
    // 手势回调方法
    @objc fileprivate func panGestureFunc(gesture:UIPanGestureRecognizer) {
        switch gesture.state {
        case .began:
            // 先获得第一次触摸点
            currentP = gesture.location(in: gesture.view)
            
        case .changed:
            // 先获得移动点
            let offset = gesture.translation(in: gesture.view)
            // 获得新的移动点
            let newTx = tabBarVc.view.transform.tx + offset.x
            
            // 判断边界
            if newTx >= barOffset{
                rightAnimation()
                return
            }
            
            if newTx <= 0 {
                leftAnimation()
                return
            }
            // 设置左边视图的移动值
            let leftNewTx = leftViewVc.view.transform.tx + offset.x * offsetScale
            // 从新设置视图的位置
            tabBarVc.view.transform = CGAffineTransform(translationX: newTx, y: 0)
            leftViewVc.view.transform = CGAffineTransform(translationX: leftNewTx, y: 0)
            // 恢复初始值
            gesture.setTranslation(CGPoint(), in: gesture.view)
            
        case .ended:
            // 获得结束时的点
            let endP = gesture.location(in: gesture.view)
            if (currentP?.x)! < endP.x {
                rightAnimation()
            }else {
                leftAnimation()
            }
        default:
            break 
        }
    }
    
    // 右边滑动动画方法
    fileprivate func rightAnimation() {
        // 添加遮盖按钮
        self.tabBarVc.view.addSubview(self.coverView)
        
        UIView.animate(withDuration: animationTime, animations: {
            // 动画移动视图
            self.tabBarVc.view.transform = CGAffineTransform(translationX: self.barOffset, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            // 设置透明度
            self.coverView.alpha = 0.6
            })
    }
    
    // 左边滑动动画
    @objc fileprivate func leftAnimation() {
        UIView.animate(withDuration: animationTime, animations: { 
            self.tabBarVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
            self.coverView.alpha = 0
            }) { (_) in
                // 取消遮盖视图
                self.coverView .removeFromSuperview()
        }
    }
}




